package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.entity.UserExtend;
import cn.stylefeng.guns.dao.service.IUserExtendService;
import cn.stylefeng.guns.modular.demo.controller.domain.QueryUserRequest;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.pqc.math.linearalgebra.RandUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.SecureRandom;
import java.sql.Wrapper;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author quspacedraon
 * @since 2024-02-21
 */
@RestController
@ApiResource(name = "用户扩展信息", resBizType = ResBizTypeEnum.BUSINESS, path = "/user/extend")
@Slf4j
public class UserExtendController {
    @Resource
    private IUserExtendService userExtendService;

    @PostResource(name = "用户扩展信息", path = "/detail", requiredLogin = true, requiredPermission = true)
    @ApiOperation("用户扩展信息")
    public ResponseData<List<UserExtend>> detail(@RequestBody QueryUserRequest queryUserRequest) {
        List<Long> userIdList = queryUserRequest.getUserIdList();
        if (CollectionUtils.isEmpty(userIdList)) {
            return new SuccessResponseData<>(Lists.newArrayList());
        }
        List<UserExtend> userExtends = userExtendService.list(Wrappers.<UserExtend>lambdaQuery().in(UserExtend::getUserId, userIdList));
        return new SuccessResponseData<>(userExtends);
    }

    @PostResource(name = "厂家信息保存", path = "/save", requiredLogin = true, requiredPermission = true)
    @ApiOperation("厂家信息保存")
    public ResponseData<Boolean> save(@RequestBody UserExtend userExtend) {
        UserExtend one = userExtendService.getOne(Wrappers.<UserExtend>lambdaQuery().eq(UserExtend::getUserId, userExtend.getUserId()));
        if (Objects.isNull(one)) {
            one = new UserExtend();
            one.setUserId(userExtend.getUserId());
        }

        one.setVender(userExtend.getVender());
        userExtendService.saveOrUpdate(one);
        return new SuccessResponseData<>(true);
    }

    @PostResource(name = "开放平台账号开通", path = "/open", requiredLogin = false, requiredPermission = false)
    @ApiOperation("开放平台账号开通")
    public ResponseData<Boolean> open(@RequestBody UserExtend userExtend) {
        UserExtend one = userExtendService.getOne(Wrappers.<UserExtend>lambdaQuery().eq(UserExtend::getUserId, userExtend.getUserId()));
        if (Objects.isNull(one)) {
            one = new UserExtend();
            one.setUserId(userExtend.getUserId());
        }
        //随机16为随机字符串

        one.setClientId(generateRandomString(16).toLowerCase());
        one.setSecret(generateRandomString(32).toLowerCase());

        userExtendService.saveOrUpdate(one);
        return new SuccessResponseData<>(true);
    }

    /**
     * 生成随机字符串
     *
     * @param length
     * @return
     */
    private String generateRandomString(int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }


}